import __init__
import sys
import os
import pandas as pd
import json
import urllib.request
from lxml import etree
import time
import execjs

import datetime as dt
import time
import random
import requests
import re
from get_data import Altas_db
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

import cons as ct

class WencaiCookie:

    def getHeXinVByHttp():
        with open(os.path.dirname(os.path.dirname(__file__)) + '/stock/hexin.js', 'r') as f:
            jscontent = f.read()
        context = execjs.compile(jscontent)
        return context.call("v")

"""

>>>ths_bk(ths_code="gn")
>>>ths_bk(ths_code="thshy")
"""
def search(query_string,pagenum=1):
    headers = {
        "Accept": "application/json,text/javascript,*/*;q=0.01",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.8",
        'Connection': 'keep-alive',
        'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
        'X-Requested-With': "XMLHttpRequest"

    }
    headers['hexin-v']=WencaiCookie.getHeXinVByHttp()
    payload = {
        "question": query_string,
        "page": pagenum,
        "perpage": 50,
        "log_info": '{"input_type": "typewrite"}',
        "source": "Ths_iwencai_Xuangu",
        "version": 2.0,
        "secondary_intent": "",
        "query_area": "",
        "block_list": "",
        "add_info": '{"urp": {"scene": 1, "company": 1, "business": 1}, "contentType": "json", "searchInfo": true}'
    }

    r = requests.post(url='http://x.10jqka.com.cn/unifiedwap/unified-wap/v2/result/get-robot-data',
                                 data=payload, headers=headers)
    #print(r.text.encode('utf-8').decode('unicode_escape'))
    try:
        result = r.json()['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
        return result
    except:
        print("error,{},{}".format(query_string,pagenum))
        result=search(query_string,pagenum=pagenum)
        return result
    #print(r.json()['data'])

def searchMain(query_string,num=50):#num不超过100
    #通过search(query_string,i) 获取数据
    df_return = pd.DataFrame()
    for i in range(num):
        time_sleep_random=random.uniform(5, 8)
        if time_sleep_random>6:
            time.sleep(5)
        time.sleep(time_sleep_random)
        if i % 21 == 20:  # 临时保存
            time.sleep(900)
        #df_return
        #并进行re化
        ct._write_console()
        json_=search(query_string,i)
        df = pd.DataFrame().from_dict(json_)
        if df.empty==True:
            # df_return=chajian.dftore(df_return)
            return df_return
        df_return = df_return.append(df, ignore_index=True)
        if i % 5 == 4:  # 临时保存
            #df_return.to_csv("{}_{}项.txt".format(query_string,len(df_return)), encoding="utf-8")
            Altas_db._save_mongo_db(df_return,"iwencai","{}_{}项".format(query_string,len(df_return)), encoding="utf-8")
    # df_return = chajian.dftore(df_return)
    return df_return
if __name__ == '__main__':
    # search.search("价格大于250天均线且换手率大于3", 100)

    #df=searchMain("20210609价格大于250天均线且换手率大于3")
    # df=searchMain('所属行业关注度排名前10',20)
    # print(df)
    # df=searchMain("20211205涨幅大于5%及概念")
    # Altas_db._save_mongo_db(df, "iwencai\zt", "{}".format(1111))
    # from data2analyse import mg
    #mg.test()

    # name="人气概念板块排名及行业"
    print(search("20210105人气概念板块排名及行业"))




    # name="成交量，5日成交量，30日成交量，120日成交量"
    # df = searchMain(name, 2)
    #
    # a=df.columns
    # print(a)
    # choose_begin_name = "区间成交量"
    # cjl="成交量"
    # list_i = []
    # for i in a:
    #     m = re.search('^{}'.format(choose_begin_name), i)
    #     if m == None:
    #         pass
    #     else:
    #         list_i.append(i)
    # sorted_id = sorted(range(len(list_i)), key=lambda k: list_i[k], reverse=False)
    # print('元素索引序列：', sorted_id)
    # data=df[[list_i[sorted_id[0]], list_i[sorted_id[1]], list_i[sorted_id[2]],'code',"股票代码","股票简称"]]
    # data.rename(columns={list_i[sorted_id[0]]:"120日",
    #                      list_i[sorted_id[1]]:"30日",
    #                      list_i[sorted_id[2]]: "5日",}, inplace=True)
    # print(data)
    # print(WencaiCookie.getHeXinVByHttp())
    # ths_bk()








